package newx.com.control.window
{
	
	import engine.scene.SceneChangeManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import newx.com.NxUIComponent;
	import newx.com.ResManager;
	import newx.com.control.IWindow;
	import newx.com.control.text.NxLabel;

	public class NxWindow extends NxUIComponent implements IWindow
	{
		private const _defaultDragType:String="DragType_Title";
		
		private var _initX			:int;
		private var _initY			:int;
		
		private var place			:Sprite;
		//边框
		protected var baseWin		:Sprite;
		
		//标题部分
		private var _titleSpace	:Sprite;
		
//		private var titleTxt		:TextField;
		
		//关闭按钮
		private var closeBtn		:SimpleButton;
		
		//帮助按钮
		private var helpBtn		:SimpleButton;
		
		//初始定位元件
		private var tPos			:Sprite;
		
		private var mainFill		:Sprite;
		
		private var mainBG			:Sprite;
		
		private var titleIconSpace	:Sprite;
		
		private var _dragSpace		:DisplayObject;
		
		//=========================================================
		
		private var _dragType		:String="DragType_Title";
		
		private var _titleSize		:uint	=	100;
		
		private var bgFillOffSet_x	:int	=	0;
		
		private var bgFillOffSet_y	:int	=	0;
		
		private var _skin			:String	=	"Panel_1";
		
		private var _windowWidth	:Number=200;
		
		private var _windowHeight	:Number=100;
		
		private var _closeBtnVisible:Boolean=true;
		
		//默认关闭
		private var _helpBtnVisible:Boolean=false;
		
		private var _textColor		:uint=0xFFFF00
		
		private var _title			:String="nxwindow";
		
		private var _titleIcon		:String	=	"";
		
		private var _titleBackVisible:Boolean	=	true;
		
		private var _titleOffsetX:Number	=	0;
		private var _stage:Stage;
		private var _titleText 	:NxLabel;
		private var _titleIconMc 	:Shape;
		
		/**界面超出比率*/		
		private var ratio:Number		=	0.75;
		
		public function NxWindow()
		{
			super();
			
			updateDisplay();
		}
		
		private function updateDisplay():void
		{
			this.clear();
			
			var skin:MovieClip=ResManager.newResClass(_skin) as MovieClip;
			
			if(null==skin)	return;
			
			place				=	new Sprite();
			
//			titleIconSpace.graphics.beginFill(0xFF8800,0);
//			titleIconSpace.graphics.drawRect(0,0,10,20);
//			titleIconSpace.graphics.endFill();
			//标题
			_titleSpace			=	skin.getChildByName("TitleSpace") as Sprite;
			//背景框
			baseWin				=	skin.getChildByName("Frame") as Sprite;
			//关闭按钮
			closeBtn	=	skin.getChildByName("Button_close") as SimpleButton;
			//帮组按钮
			helpBtn		=	skin.getChildByName("Button_help") as SimpleButton;
			addChild(place);
			place.addChild(baseWin);
			place.addChild(_titleSpace);
			
			//标题艺术字背景
			titleIconSpace		=	new Sprite();
			place.addChild(titleIconSpace);
			titleIconSpace.mouseChildren = titleIconSpace.mouseEnabled = false;
			
		
		
			_titleText =	new NxLabel();
			_titleText.text			=	""
			titleIconSpace.addChild(_titleText);
			_titleIconMc = new  Shape();
			titleIconSpace.addChild(_titleIconMc);
						
			place.addChild(closeBtn);
			place.addChild(helpBtn);
			
			helpBtnVisible = _helpBtnVisible;
			closeBtnVisible = _closeBtnVisible;

//			_titleSpace.visible	=	_titleBackVisible;
			updateMainBG(_windowWidth,_windowHeight);
			updateTitle();
			addEvent();
			
			_initX = this.x;
			_initY = this.y;
		}
		
		override public function set x(value:Number):void
		{
			// TODO Auto Generated method stub
			super.x = value;
			
			_initX = value;
		}
		
		override public function set y(value:Number):void
		{
			// TODO Auto Generated method stub
			super.y = value;
			_initY = value;
		}
		
		
		private function clear():void
		{
			while(this.numChildren>0)
			{
				this.removeChildAt(0);
			}
			removeEvent();
			
			_titleSpace=null;
			closeBtn=null;
			helpBtn=null;
			tPos=null;
			mainFill=null;
			mainBG=null;
			baseWin=null;
		}
		
		private function titleMouseDownHandler(e:MouseEvent):void
		{
			startDrag(false);
			_stage	=	DisplayObject(e.target).stage;
			if(_stage)
				_stage.addEventListener(MouseEvent.MOUSE_UP,	titleMouseUpHandler);
		}
		private function titleMouseUpHandler(e:MouseEvent):void
		{
			stopDrag();
			if(_stage)
			{
				_stage.removeEventListener(MouseEvent.MOUSE_UP,	titleMouseUpHandler);
				checkPosition();
			}
		}
		
		/**
		 * 界面超出处理
		 */		
		private function checkPosition():void
		{
			var point:Point	=	this.parent.localToGlobal(new Point(x,y));
			if( point.x+this.parent.width*ratio< 100     || point.x+this.parent.width*(1-ratio)>SceneChangeManager.getGameWidth() 
				|| point.y+this.parent.height*ratio< 0 || point.y+this.parent.height*(1-ratio)>SceneChangeManager.getGameHeight() )
			{
				resetPoint();
			}
		}
		
		private function removeEvent():void
		{
			if(closeBtn)
			{
				closeBtn.removeEventListener(MouseEvent.CLICK,onClickClose);
			}
			removeDragEvent();
		}
		
		private function addEvent():void
		{
			if(closeBtn)
			{
				closeBtn.addEventListener(MouseEvent.CLICK,onClickClose);	
			}
			addDragEvent();
		}
		
		private function addDragEvent():void
		{
			switch(_dragType)
			{
				case NxWindowDragType.Window_ALL:
					_dragSpace	=	place;
					break;
				case NxWindowDragType.Window_TITLE:
					_dragSpace	=	_titleSpace;
					break;
				default:
					_dragSpace	=	null
					break;
			}
			
			if(_dragSpace != null)
			{
				_dragSpace.addEventListener(MouseEvent.MOUSE_DOWN,		titleMouseDownHandler);
			}
		}
		
		private function removeDragEvent():void
		{
			if(_dragSpace != null)
			{
				_dragSpace.removeEventListener(MouseEvent.MOUSE_DOWN,		titleMouseDownHandler);
			}
		}
		
		private function onClickClose(e:MouseEvent):void
		{
			if( e.target == closeBtn )
			{
				dispatchEvent(new NxWindowEvent(NxWindowEvent.WINDOW_CLOSE));
			}else if( e.target == helpBtn )
			{
				dispatchEvent(new NxWindowEvent(NxWindowEvent.WINDOW_HELP));
			}
			
		}
		
		private function updateTitle():void
		{
			if(_titleSpace)
			{
				var titleBtn:DisplayObject	=	_titleSpace.getChildByName("Button_title");
				
				titleBtn.width		=	titleIconSpace.width;
			
				if(	(titleBtn.width + _titleOffsetX)	<	_titleSize	)
				{
					titleBtn.width	=	_titleSize;
				}
				else
				{
					titleBtn.width		=	titleBtn.width + _titleOffsetX;
				}
//				
				titleBtn.x			=	-0.5	*	titleBtn.width;

//				//刷新标题容器
				_titleSpace.x		=	_windowWidth*0.5
				_titleSpace.y		=	0;
			}
			updateTitleIcon();
			
		}
		
		private function updateMainBG(w:uint,h:uint):void
		{
			if(baseWin)
			{
				baseWin.width=w;
				baseWin.height=h;
			}
			//关闭按钮
			if(closeBtn)
			{
				closeBtn.x	=	w - closeBtn.width - 10;
				closeBtn.y	=	10;
			}
			if(helpBtn)
			{
				helpBtn.x	=	closeBtn.x - helpBtn.width - 6;
				helpBtn.y	=	10;	
			}
		}
		/**
		 * 更新标题图标坐标 
		 */		
		private function updateTitleIcon():void
		{
			titleIconSpace.x	=	(_windowWidth - titleIconSpace.width) * .5;
			titleIconSpace.y	=	8;
		}
		
		/** 
		 * 老接口，尽量不用
		 */		
		public function initDragSpace():void
		{
			this.x	=	this.y	=	0;
		}
		
		public function resetPoint():void
		{
			this.x = _initX;
			this.y = _initY;
		}
		
		public function set closeBtnVisible(val:Boolean):void
		{
			if( val == closeBtn.visible )	return;
			
			_closeBtnVisible = val;
			
			btnVisible( closeBtn, _closeBtnVisible );
		}
		
		public function get closeBtnVisible():Boolean
		{
			return _closeBtnVisible;
		}
		
		public function set helpBtnVisible(val:Boolean):void
		{
			if( val == helpBtn.visible )	return;
			
			_helpBtnVisible=val;
			
			btnVisible( helpBtn, _helpBtnVisible );
		}
		//
		public function get nxWindowhelpBtn():SimpleButton
		{
			return helpBtn
		}
		private function btnVisible( btn : SimpleButton, visible : Boolean ):void
		{
			if( btn )
			{
				btn.visible = visible;
				
				if( visible )
				{
					if( !btn.hasEventListener( MouseEvent.CLICK ) )
					{
						btn.addEventListener(MouseEvent.CLICK, onClickClose);
					}
				}
				else
				{
					if( btn.hasEventListener( MouseEvent.CLICK ) )
					{
						btn.removeEventListener(MouseEvent.CLICK, onClickClose);
					}
				}
			}
		}
		
		public function get helpBtnVisible():Boolean
		{
			return _helpBtnVisible;
		}
		
		public function set dragSpaceType(val:String):void
		{
			_dragType	=	val;
			removeDragEvent();
			addDragEvent();
		}
		
		public function get dragSpaceType():String
		{	
			return _dragType;
		}
		/**
		 * 无用的功能了，现在这个武侠版不用了 
		 * @param str
		 * 
		 */			
		public function set title(str:String):void
		{
			_title			=	str;
//			
//			if(_titleSpace)
//			{
//				var titleTxt:TextField=_titleSpace.getChildByName("txt") as TextField;
//				titleTxt.htmlText	=	str;
//				updateTitle();
//			}
		}
		
		public function get title():String
		{
			return _title;
		}
		
		public function set titleHGap(val:Number):void
		{
			_titleOffsetX	=	val;
			
			updateTitle();
		}
		
		public function get titleHGap():Number
		{
			return _titleOffsetX
		}
		
		
		override public function set width(val:Number):void
		{
			_windowWidth	=	val;
			if(baseWin)
			{
				updateMainBG(_windowWidth,_windowHeight);
				updateTitle();
			}
		}
		
		override public function get width():Number
		{
			return _windowWidth;
		}
		
		override public function set height(val:Number):void
		{
			_windowHeight	=	val;
			
			if(baseWin)
			{
				updateMainBG(_windowWidth,_windowHeight);
				updateTitle();
			}
		}
		
		override public function get height():Number
		{
			return _windowHeight;
		}
		
		public function get titleSize():uint
		{
			return _titleSize;
		}
		
		public function set titleSize(val:uint):void
		{
			_titleSize	=	val;
			updateTitle();
		}
		
		public function set skin(str:String):void
		{
			_skin=str;
			
			updateDisplay();
		}
		
		public function get skin():String
		{
			return _skin;
		}
		
		/** 
		 * 删除窗体
		 */		
		public function remove():void
		{
			removeEvent();
			
			if(this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
		
		override public function hide():void
		{
			remove();
		}
		
		public function get color():uint
		{
			return _textColor;
		}
		
		public function set color(val:uint):void
		{
			_textColor=val;
			
			updateTitle();
		}
		
		public function setTextFormat(format:TextFormat, beginIndex:int=-1, endIndex:int=-1):void
		{
			if(_titleSpace)
			{
				var titleTxt:TextField=_titleSpace.getChildByName("txt") as TextField;
				titleTxt.setTextFormat(format, beginIndex, endIndex);
			}
		}
		
		public function getTextFormat(beginIndex:int=-1, endIndex:int=-1):TextFormat
		{
			if(_titleSpace)
			{
				var titleTxt:TextField=_titleSpace.getChildByName("txt") as TextField;
				return titleTxt.getTextFormat(beginIndex, endIndex);
			}
			return null;
		}

		public function get titleIcon():String
		{
			return _titleIcon;
		}

		public function set titleIcon(value:String):void
		{
			_titleIcon = value;
			var _titleMc:MovieClip	=	ResManager.newResClass(
				_titleIcon) as MovieClip;
			if(_titleMc != null)
			{
				titleIconSpace.removeChildAt(1);
				titleIconSpace.addChild(_titleMc.getChildAt(0));
//				_titleIconMc  =   _titleMc.getChildAt(0) as Shape 
			}
			updateTitleIcon();
//			updateDisplay();
		}

		public function get titleBackVisible():Boolean
		{
			return _titleBackVisible;
		}
		
		public function set titleBackVisible(value:Boolean):void
		{
			_titleBackVisible = value;
			_titleSpace.visible	=	_titleBackVisible;
		}


	}
}